/* eslint-disable */
/**
 * 将数据整成树型结构
 * @param data
 * @param parentKey
 * @param key
 * @returns {any[]}
 */
export default function getTreeData(data, parentKey='parentId', key='orgId') {
  const list = JSON.parse(JSON.stringify(data))
  function getFather() {
    var arr = [];
    list.forEach(function(item) {
      if (!list.some((value) => item[parentKey] === value[key])) {
        arr.push({...item, isOutermost: true});
      }
    });
    return arr;
  }
  function getChildren(item) {
    var brr = JSON.parse(JSON.stringify(item));
    list.forEach((value) => {
      if (value[parentKey] === item[key]) {
        if (!brr.children) brr.children = [];
        brr.children.push(getChildren(value));
      }
    });
    return brr;
  }
  return getFather().map(function(value) {
    return getChildren(value);
  });
}

/**
 * 将树型结构的数据拍扁整成一维数组
 * @param data
 * @param children
 * @returns {[]}
 */
export function treeToArray(data, children = 'children') {
  let tmp = []
  data.forEach((item, index) => {
    tmp.push(item)
    if (item[children] && item[children].length > 0) {
      const res = treeToArray(item[children], children)
      tmp = tmp.concat(res)
    }
  })
  return tmp
}

